package com.realestate.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
import com.realestate.constants.ComUnitName;
import com.realestate.constants.RecordStatus;
import com.realestate.model.BrtbProductAttributeValue;
import com.realestate.model.QBrtbAttribute;
import com.realestate.model.QBrtbProduct;
import com.realestate.model.QBrtbProductAttributeValue;
import com.trg.dao.jpa.GenericDAOImpl;

@Repository
public class ProductAttributeValueDaoImpl extends GenericDAOImpl<BrtbProductAttributeValue, Long> implements ProductAttributeValueDao{
	
	@SuppressWarnings("unused")
	private static final Logger LOGGER = Logger.getLogger(ProductAttributeValueDaoImpl.class);
	
	@Override
	@PersistenceContext(unitName = ComUnitName.COM_UNIT)
	public void setEntityManager(EntityManager entityManager) {
		super.setEntityManager(entityManager);
	}

	@Override
	public List<Object[]> getAllAttributesOfAProduct(Long productId) {
		JPQLQuery query = new JPAQuery(em());
		
		QBrtbProduct product = QBrtbProduct.brtbProduct;
		QBrtbAttribute attribute = QBrtbAttribute.brtbAttribute;
		QBrtbProductAttributeValue attributeValue = QBrtbProductAttributeValue.brtbProductAttributeValue;
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(product.recordStatus.eq(RecordStatus.O))
			 .and(attribute.recordStatus.eq(RecordStatus.O))
			 .and(product.id.eq(productId));
		
		query.from(attributeValue)
			.leftJoin(attributeValue.brtbProduct, product)
			.leftJoin(attributeValue.brtbAttribute, attribute)
			.where(where);
		
		List<Object[]> products = query.list(product.id, product.productCode, product.productName, attribute.attributeCode, attribute.attributeName, attributeValue.strValue);
		return products;
	}

	@Override
	public List<BrtbProductAttributeValue> deleteAllAttributeValueOfAProduct(Long productId) {
		JPQLQuery query = new JPAQuery(em());
		
		QBrtbProduct product = QBrtbProduct.brtbProduct;
		QBrtbProductAttributeValue attributeValue = QBrtbProductAttributeValue.brtbProductAttributeValue;
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(product.recordStatus.eq(RecordStatus.O))
			 .and(product.id.eq(productId));
		
		query.from(attributeValue)
			.leftJoin(attributeValue.brtbProduct, product)
			.where(where);
		
		List<BrtbProductAttributeValue> products = query.list(attributeValue);
		return products;
	}

	@Override
	public BrtbProductAttributeValue getByProductIdAndAttributeId(
			Long productId, Long attributeId) {
		JPQLQuery query = new JPAQuery(em());
		
		QBrtbProduct product = QBrtbProduct.brtbProduct;
		QBrtbAttribute attribute = QBrtbAttribute.brtbAttribute;
		QBrtbProductAttributeValue attributeValue = QBrtbProductAttributeValue.brtbProductAttributeValue;
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(product.recordStatus.eq(RecordStatus.O))
			 .and(product.id.eq(productId))
			 .and(attribute.recordStatus.eq(RecordStatus.O))
			 .and(attribute.id.eq(attributeId))
			 .and(attributeValue.brtbAttribute.id.eq(attributeId))
			 .and(attributeValue.brtbProduct.id.eq(productId));
		
		query.from(attributeValue, product, attribute)
			.where(where);
		
		BrtbProductAttributeValue result = query.uniqueResult(attributeValue);
		return result;
	}
}
